package com.origin.onlinesys.common.services;

import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2015/1/20.
 * 业务层基类
 */
public abstract class BaseService<T> {

    @Autowired
    protected Mapper<T> mapper;

    public int save(T t){
        return mapper.insert(t);
    }

    public int delete(T t){
        return mapper.delete(t);
    }

    public List<T> queryAll(){
        return mapper.select(null);
    }

    public List<T> queryByWhere(T t){
        return mapper.select(t);
    }

    /**
     * 根据条件查询返回单条数据
     * @param t
     * @return
     */
    public T selectSingleByWhere(T t){
        List<T> list = mapper.select(t);
        if(list != null && list.size() != 0)
            return list.get(0);
        return null;
    }

    public List<T> selectByWhere(T t){
        return mapper.select(t);
    }


    public int update(T t){
        return mapper.updateByPrimaryKeySelective(t);
    }

    /**
     * 没有条件分页查询
     * @param pageNum  第几页
     * @param pageSize 每页多少条
     * @return
     */
    public Page<T> selectByPage(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize,true);
        Page<T> list = (Page)mapper.select(null);
        return list;
    }

    /**
     * 条件分页查询
     * @param pageNum  第几页
     * @param pageSize 每页多少条
     * @return
     */
    public Page<T> selectByPage(int pageNum, int pageSize,T t){
        PageHelper.startPage(pageNum, pageSize, true);
        Page<T> list = (Page)mapper.select(t);
        return list;
    }


}
